/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display.particle;

import com.jogamp.opengl.util.Animator;
import deatax.display.Texture;
import deatax.display.TileRenderer;
import java.awt.Dimension;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;

/**
 *
 * @author nrechten
 */
public class ParticleTester extends GLCanvas implements GLEventListener {

    ParticleSpawn ps;

    public ParticleTester() {
        this.addGLEventListener(this);
        this.setPreferredSize(new Dimension(600,600));

//         ps = new ParticleSpawn(new float[]{0,0,0}, 10, 2f, 10, 1,new ParticleRule[]{
//            new VectorRule(new float[]{0,1.5f,0}, new float[]{0.6f,-1,0}, 0.5f, 0, 0.8f)
//            ,new AlphaRule(-0.2f, 0, 1)
//            ,new ScaleRule(0.1f, 0.1f, 2)
//        }, new Texture("display/particle/smoke.png"));
         
         ps = new ParticleSpawn(new float[]{0, 0, 0}, 50, 0.5f, 10, 1, new ParticleRule[]{
                    new VectorRule(new float[]{0, 0, 1f}, new float[]{0.8f, 0, -1}, 0.5f, 0, 0.8f),
                    new AlphaRule(-0.3f, 0, 1),
                    new ScaleRule(0.1f, 0.1f, 2)
                }, new Texture("display/particle/smoke.png"));
        
        Animator a = new Animator(this);
        a.start();
    }

    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClearColor(0, 1, 0, 1);
        gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
        gl.glDepthFunc(GL.GL_LEQUAL);
        gl.glEnable(GL2.GL_DEPTH_BUFFER);
        gl.glEnable(GL2.GL_BLEND);
    }

    public void dispose(GLAutoDrawable drawable) {
    }

    long last = 0;

    float[]  circle = TileRenderer.genPolygon(6, 1);
    
    public void display(GLAutoDrawable drawable) {

        GL2 gl = drawable.getGL().getGL2();
        GLU glu = new GLU();


        
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        glu.gluLookAt(0, 0, -20, 0, 0, 0, 0, 1, 0);

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);

        gl.glColor3f(1, 1, 1);

        gl.glBegin(GL2.GL_LINE_LOOP);
        gl.glVertex3f(-5, -5, 0);
        gl.glVertex3f(5, -5, 0);
        gl.glVertex3f(5, 5, 0);
        gl.glVertex3f(-5, 5, 0);
        gl.glEnd();
        
        gl.glBegin(GL2.GL_LINE_LOOP);
        for (int i = 0; i < circle.length ; i += 2) {
            gl.glVertex3f(circle[i], circle[i + 1],5);
        }
        gl.glEnd();

        if(last != 0){
            ps.draw((System.currentTimeMillis()-last)/1000f, gl);
        }
        last = System.currentTimeMillis();

        gl.glFlush();
//        drawable.swapBuffers();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL2 gl = drawable.getGL().getGL2();
        GLU glu = new GLU();

        gl.glViewport(0, 0, width, height);

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, width/height, 1f, 100.0f);
    }

    public static void main(String args[] ){
        JFrame frame = new JFrame();


        frame.add(new ParticleTester());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
